package com.coder.oj.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Discuss;
import com.coder.oj.common.mapper.DiscussMapper;
import com.coder.oj.common.validate.ValidationGroups;
import com.coder.oj.system.dto.DiscussDto;
import com.coder.oj.system.mapstruct.DiscussMapstruct;
import com.coder.oj.system.query.DiscussQuery;
import com.coder.oj.system.service.DiscussService;
import com.coder.oj.system.utils.CheckUserUtils;
import com.coder.oj.system.vo.DiscussVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"评论管理"})
@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Autowired
    private DiscussService discussService;

    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private DiscussMapstruct discussMapstruct;

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<DiscussVo>> getDiscussPage(@RequestBody DiscussQuery params) {
        return R.ok().setResult(discussService.getDiscussPage(params));
    }

    @GetMapping
    @ApiOperation("评论详情")
    public R<DiscussVo> getDiscussById(@RequestParam("id") Long id) {
        return R.ok().setResult(discussMapstruct.toVO(discussMapper.selectById(id)));
    }

    @JwtToken
    @PostMapping
    @ApiOperation("新增评论")
    public R<DiscussVo> saveDiscuss(@Validated(ValidationGroups.Insert.class) @RequestBody DiscussDto params) {
        discussMapper.insert(discussMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @DeleteMapping
    @ApiOperation("删除评论")
    public R deleteDiscussById(@RequestParam("id") Long id) {
        discussService.deleteDiscussById(id);
        return R.ok();
    }
}
